package com.poetize.plus.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.poetize.plus.entity.dto.BackgroundImageCheckDTO;
import com.poetize.plus.entity.dto.BackgroundImageDTO;
import com.poetize.plus.entity.dto.page.BackgroundImagePageDTO;
import com.poetize.plus.entity.po.BackgroundImage;
import com.poetize.plus.entity.vo.page.BackgroundImagePageVO;
import com.poetize.plus.mapper.BackgroundImageMapper;
import com.poetize.plus.service.BackgroundImageService;
import com.poetize.plus.utils.BaseConstant;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
import java.util.Random;

/**
 * @author zuosy
 * @created 2024/4/10 11:23
 */
@Service
public class BackgroundImageServiceImpl extends ServiceImpl<BackgroundImageMapper, BackgroundImage> implements BackgroundImageService {

    @Override
    public Page<BackgroundImagePageVO> imagePage(BackgroundImagePageDTO backgroundImagePageDTO) {
        Page<BackgroundImage> imagePage = lambdaQuery()
                .page(Page.of(backgroundImagePageDTO.getCurrent(), backgroundImagePageDTO.getSize()));
        Page<BackgroundImagePageVO> result = new Page<>();
        if (CollectionUtils.isEmpty(imagePage.getRecords())){
            return result;
        }
        result.setTotal(imagePage.getTotal());
        List<BackgroundImagePageVO> voList = new ArrayList<>();
        imagePage.getRecords().forEach(e -> {
            BackgroundImagePageVO vo = new BackgroundImagePageVO(
                    e.getId(),
                    e.getPath(),
                    e.getFilename(),
                    e.getCreateTime());
            voList.add(vo);
        });
        result.setRecords(voList);
        return result;
    }

    @Override
    public Boolean checkImage(BackgroundImageCheckDTO backgroundImageCheckDTO) {
        if (!BaseConstant.Resource.imageType.contains(backgroundImageCheckDTO.mimeType())){
            throw new IllegalArgumentException("文件类型错误");
        }
        BackgroundImage checkFilename = lambdaQuery()
                .eq(BackgroundImage::getFilename, backgroundImageCheckDTO.filename())
                .one();
        if (Objects.nonNull(checkFilename)){
            throw new IllegalArgumentException("该文件已存在");
        }
        return true;
    }

    @Override
    public Boolean add(BackgroundImageDTO backgroundImageDTO) {
        BackgroundImage backgroundImage = BackgroundImageDTO.convertDTO(backgroundImageDTO);
        return baseMapper.insert(backgroundImage) > 0;
    }

    @Override
    public String getRandomBackgroundImage() {
        List<BackgroundImage> imageList = lambdaQuery().list();
        if (CollectionUtils.isEmpty(imageList)){
            return null;
        }
        Random random = new Random();
        int randomNumber = random.nextInt(imageList.size());
        BackgroundImage backgroundImage = imageList.get(randomNumber);
        return backgroundImage.getPath();
    }
}
